$(function () {
    var $form = $("#lj_wlyForm");
    //数据字典-加载
    $form.find("[idVal='lj_jsfs_id']").sjzdcombobox("khjsfs");
    //物流园-加载
    $form.find("[idVal='lj_jyjg_id']").jygsjgcombotree({editable: false, panelWidth: 200, onClick: function () {
        $form.find("[idVal='lj_mc']").val(createMc())
            .next().html(createMc());
        $("#lj_ljjc").val(createJc());
    }});
    //上游-加载
    var strSyId = $form.find("[idVal='lj_sy_id']").attr("data-value");
    var strSyJc = $form.find("[idVal='lj_sy_id']").attr("data-text");
    var arrSySelect = [];
    if (strSyJc) {
        var arrSyId = strSyId.split(",");
        var arrSyJc = strSyJc.split(",");
        if (strSyJc != "") {
            for (var i = 0; i < arrSyId.length; i++) {
                arrSySelect.push({id: arrSyId[i], jc: arrSyJc[i]})
            }
        }
    }
    $form.find("[idVal='lj_sy_id']").qyjgwin({
        selected: arrSySelect,
        multiple: true,
        callback: function ($target) {
            $form.find("[idVal='lj_mc']").val(createMc()).next().html(createMc());
            $("#lj_ljjc").val(createJc());
        }
    });
    //下游-加载
    var strXyId = $form.find("[idVal='lj_xy_id']").attr("data-value");
    var strXyJc = $form.find("[idVal='lj_xy_id']").attr("data-text");
    var arrXySelect = [];
    if (strXyJc) {
        var arrXyId = strXyId.split(",");
        var arrXyJc = strXyJc.split(",");
        if (strXyJc != "") {
            for (var i = 0; i < arrXyId.length; i++) {
                arrXySelect.push({id: arrXyId[i], jc: arrXyJc[i]})
            }
        }
    }
    $form.find("[idVal='lj_xy_id']").qyjgwin({
        selected: arrXySelect, //[{id:123,jc:"张三"},{id:1234,jc:"张三1"}],
        multiple: true,
        callback: function ($target) {
            $form.find("[idVal='lj_mc']").val(createMc()).next().html(createMc());
            $("#lj_ljjc").val(createJc());
        }
    });

    if ($form.find("[idval='xshtdycombox']")) {
        $form.find("[idval='xshtdycombox']").htdycombobox(false, 'xsht');
    }


    var createJc = function () {
        var jyjg = $form.find("[idVal='lj_jyjg_id']").combotree("getText");
        var yljsy = $form.find("[idVal='lj_sy_id']").searchWin();
        var yljxy = $form.find("[idVal='lj_xy_id']").searchWin();
        if (yljsy) {
            var ljsy = yljsy.getText();
        } else {
            var ljsy = '';
        }
        if (yljxy) {
            var ljxy = yljxy.getText();
        } else {
            var ljxy = '';
        }

        var ljsy1 = ljsy;
        var ljxy1 = ljxy;
        ljsy1 = ljsy1 ? ("上游名称为：" + ljsy1) : "";
        ljxy1 = ljxy1 ? ("下游名称为：" + ljxy1) : "";
        return ljsy1 + "," + ljxy1;
    }

    var createMc = function () {
        var jyjg = $form.find("[idVal='lj_jyjg_id']").combotree("getText");
        var yljsy = $form.find("[idVal='lj_sy_id']").searchWin();
        var yljxy = $form.find("[idVal='lj_xy_id']").searchWin();
        if (yljsy) {
            var ljsy = yljsy.getText();
        } else {
            var ljsy = '';
        }
        if (yljxy) {
            var ljxy = yljxy.getText();
        } else {
            var ljxy = '';
        }

        var ljsy1 = ljsy;
        var ljxy1 = ljxy;
        var mc = "(1)运营商上下游关系((2),(3))";
        ljsy1 = ljsy1 ? ("上游名称为：" + ljsy1) : "";
        ljxy1 = ljxy1 ? ("下游名称为：" + ljxy1) : "";
        return mc.replace("(1)", jyjg).replace("(2)", ljsy1).replace("(3)", ljxy1);
    }


    $("#lj-uploadify").upload('lj-fileQueue', {
        onUploadSuccess: function (file, data) {
            var obj = eval('(' + data + ')');
            $("#lj-fileQueue .data").remove();
            $("#lj-fileQueue .uploadify-progress").fadeOut(500, function () {
                $(this).remove();
            });
            $("#" + file.id).append("<input type='hidden' id='lj-file" + obj.fileId + "' name='lj.fjList.id' value='" + obj.fileId + "'/>");
            $("#" + file.id + " .cancel a").bind('click', function () {
                $(this).attr('href', 'javascript:void(0)');
                deleteLjFile(obj.fileId, file.id);
            });
        }
    });
});

function deleteLjFile(fileId, id) {
    $.messager.confirm('确认', '是否确认删除此附件', function (r) {
        if (r) {
            $.post($.webPath() + '/base/fj/deleteFj', {'fjId': fileId}, function (data) {
                var obj = eval('(' + data + ')');
                if (obj.statusCode == '200') {
                    $("#lj-file" + fileId).remove();
                    $("#" + id).fadeOut(500, function () {
                        $(this).remove();
                    });
                }
            });
        }
    })
}


